const models = require('../db/mysqldb/index')
const logger = require('../utils/logger')

class ChatGroupMessageModel {
    /**
     * @description 获取当前用户所有相关聊天信息列表
     * @param {string} userId
     * @returns
     */
    static async findByUserId (user_id) {
        let sql = `SELECT
                    cgm.from,
                    cgm.content as message,
                    cgm.msg_type as chat_type,
                    cgm.create_timestamp as message_time,
                    cgi.group_id,
                    cgi.group_name,
                    cgi.group_avatar,
                    cgi.create_timestamp AS group_create_time,
                    cgur.user_alias AS user_alias,
                    cgur.top AS top,
                    su.realname,su.sex,su.phone,
                    su.avatar,
                    (
                      select count(1) from chat_group_message ucgm WHERE ucgm.delete_timestamp is null and ucgm.to = ${user_id} and ucgm.read = 0
                    ) as unread
                FROM
                    chat_group_message AS cgm
                LEFT JOIN
                    chat_group_info AS cgi
                ON
                    cgm.group_id = cgi.group_id
                LEFT JOIN
                    chat_group_user_relation AS cgur
                ON
                    cgm.from = cgur.user_id
                INNER JOIN
                    sys_user AS su
                ON
                    cgm.from = su.user_id
                WHERE
                    cgm.delete_timestamp IS NULL
                AND (
                        cgm.from = ${user_id}
                    OR  cgm.to = ${user_id})
                ORDER BY
                    cgm.create_timestamp DESC
                      LIMIT 1`
        const result = await models.sequelize.query(sql, {
            replacements: ['active'],
            type: models.sequelize.QueryTypes.SELECT,
        })
        return result
    }

    /**
     * @description 获取当前用户所有相关聊天信息列表
     * @param {string} userId
     * @returns
     */
    static async getList (group_id, limit, size, user_id) {
        const sql = `SELECT * FROM 
          (SELECT g.content, g.create_timestamp, g.from,i.avatar,i.realname FROM chat_group_message As g 
            inner join sys_user AS i ON g.from = i.user_id 
            WHERE g.to = ${group_id} and g.from = ${user_id} and g.to = ${user_id} and g.delete_timestamp is null order by g.create_timestamp desc limit ${limit},${(size - 1) * limit}) as n order by n.create_timestamp`;
        logger.info(`ChatGroupMessageModel`, `getList`, `sql`, sql)
        const result = await models.sequelize.query(sql, {
            replacements: ['active'],
            type: models.sequelize.QueryTypes.SELECT,
        })
        return result
    }

    /**
       * @description 保存聊天信息
       * @param {string} group_id
       * @param {string} user_id
       * @param {string} content
       * @returns
       */
    static async saveMsg (group_id, user_id, content, groupMember) {
        const bsql = `INSERT INTO chat_group_message (chat_group_message.from,group_id,chat_group_message.to,content,create_timestamp) VALUES `;
        let isql = groupMember.array.map(item => {
            return `(${user_id},${group_id},${item},${content},${Date.now()})`
        })
        const sql = bsql + isql.join(' ')
        return await models.sequelize.query(sql)
    }
}
module.exports = {
    ChatGroupMessageModel,
}
